package com.zklh.test;

class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
        if(root==null)return null;
        if(root.val==key){
            if(root.right==null){
                return root.left;
            }
            if(root.left ==null){
                return root.right;
            }
            int val= find_max(root.left);
            root.val = val;
            root.left = deleteNode(root.left,val);
        }else {
            if(root.val<key){
                root.right = deleteNode(root.right,key);}
            if(root.val>key){
                root.left = deleteNode(root.left,key);}
        }
        return root;
    }
    public int find_max(TreeNode root){

        while(root.right!=null){
            root = root.right ;
        }
        return root.val;
    }
}